import { getFunction, getValue, setValue } from './util';
import Watcher from './watcher';

export default class Compiler {
  constructor(vm) {
    this.el = vm.$el;
    this.vm = vm;
    this.compile(vm.$el);
  }
  compile(el) {
    const childNodes = el.childNodes;
    for (const node of [...childNodes]) {
      if (this.isTextNode(node)) {
        this.compileText(node);
      } else if (this.isElementNode(node)) {
        this.compileElement(node);
      }
      if (node.childNodes && node.childNodes.length > 0) {
        this.compile(node);
      }
    }
  }
  compileElement(node) {
    for (const attr of node.attributes) {
      let attrName = attr.name;
      if (this.isDirective(attrName)) {
        const key = attr.value;
        if (attrName.startsWith('@') || attrName.startsWith('v-on:')) {
          let value
          // 如果返回的是函数表达式，就用高阶函数包裹
          if (this.isFunction(key)) {
            value = getFunction(this.vm, key);
          } else {
            value = getValue(this.vm, key);
          }
          attrName = attrName.replace('@', '').replace('v-on:', '');
          this.eventHandler(node, attrName, value);
        } else {
          const value = getValue(this.vm, key);
          attrName = attrName.substr(2);
          this.update(node, key, attrName, value);
        }
      }
    }
  }
  update(node, key, attrname, value) {
    let updateFn = this[`${attrname}Updater`];
    updateFn && updateFn.call(this, node, key, value);
  }
  textUpdater(node, key, value) {
    node.textContent = value;
    new Watcher(this.vm, key, (newValue) => {
      node.textContent = newValue;
    });
  }
  htmlUpdater(node, key, value) {
    node.innerHTML = value;
    new Watcher(this.vm, key, (newValue) => {
      node.innerHTML = newValue;
    });
  }
  eventHandler(node, eventName, listener) {
    console.log(node, eventName, listener);
    node.addEventListener(eventName, listener);
  }
  modelUpdater(node, key, value) {
    node.value = value;
    new Watcher(this.vm, key, (newValue) => {
      node.value = newValue;
    });
    node.addEventListener('change', (e) => {
      const value = e.target.value;
      if (getValue(this.vm, key) !== value) {
        setValue(this.vm, key, value);
      }
    });
    node.addEventListener('input', (e) => {
      const value = e.target.value;
      if (getValue(this.vm, key) !== value) {
        setValue(this.vm, key, value);
      }
    });
  }
  compileText(node) {
    const reg = /\{\{(.+?)\}\}/;
    const value = node.textContent;
    if (reg.test(value)) {
      const key = RegExp.$1.trim();
      const value = getValue(this.vm, key);
      node.textContent = value.replace(reg, value);
      new Watcher(this.vm, key, (newValue) => {
        node.textContent = newValue;
      });
    }
  }
  isDirective(attrName) {
    return attrName.startsWith('v-') || attrName.startsWith('@');
  }
  isTextNode(node) {
    return node.nodeType === 3;
  }
  isElementNode(node) {
    return node.nodeType === 1;
  }
  isFunction(key) {
    // 如果含有括号，就是函数表达式
    return /^.+\(.*\)$/.test(key)
  }
}
